<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="shortcut icon" href="../../../favicon.ico" type="image/x-icon">
	<title>life_cycle</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../../../js/17/react.development.js" ></script>
  <script type="text/javascript" src="../../../js/17/react-dom.development.js"></script>
  <script type="text/javascript" src="../../../js/17/babel.min.js"></script>
  <script type="text/javascript" src="../../../js/17/prop-types.js"></script>

  <script type="text/babel">
    class Count extends React.Component {
      state = {
        count: 0
      }
      constructor(){
        super()
        console.log('count-constructor');
      }
      
      static getDerivedStateFromProps(props, state){
        console.log('count--getDerivedStateFromProps', props, state);
        return null
      }

      getSnapshotBeforeUpdate(state){
        console.log('count--getSnapshotBeforeUpdate', state);
        return null
      }

      shouldComponentUpdate(){
        console.log('count--shouldComponentUpdate')
        return true
      }

      componentDidUpdate(prevProps, prevState, snapshotValue){
        console.log('count--componentDidUpdate', prevProps, prevState, snapshotValue)
      }

      componentDidMount(){
        console.log('count--componentDidMount')
      }

      componentWillUnmount(){
        console.log('count--componentWillUnmount')
      }

      add=()=>{
        let {count} = this.state
        count+=1
        this.setState({count})
      }

      unmount=()=>{
        console.log('count--unmount');
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }

      forceUpd=()=>{
        console.log('count--forceUpd');
        this.forceUpdate();
      }

      render(){
        console.log('count--render')
        const {count} = this.state
        return (
          <div>
            <h2>当前求和为: {count}</h2>
            <button onClick={this.add}>点我+1</button>
            <button onClick={this.unmount}>卸载组件</button>
            <button onClick={this.forceUpd}>不更改数据,强制更新页面</button>
          </div>
        )
      }
    }

    ReactDOM.render(<Count count={1}/>, document.getElementById('test'));
  </script>
</body>